package com.example.cardandswitch.swtch;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.example.cardandswitch.R;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

public class SwitchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);

        SwitchCompat sw = findViewById(R.id.switch1);

        sw.setError("错误");      //设置错误信息的显示
        sw.setTextOff("off");     //设置关闭时的文字
        sw.setTextOn("on");       //设置打开时的文字
        sw.setShowText(true);     //设置是否显示文字
        sw.setSwitchPadding(20);  //设置文字与开关的间隙
        sw.setChecked(true);      //设置是否是选中(打开)状态
        //设置打开关闭监听
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    buttonView.setError(null);
                    Toast.makeText(SwitchActivity.this, "on", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(SwitchActivity.this, "off", Toast.LENGTH_SHORT).show();
            }
        });
//        sw.setThumbTintList();
//        sw.setEnabled(false);

        SwitchCompat switch1 = findViewById(R.id.switch2);
        switch1.setChecked(true);
        switch1.setSplitTrack(true);
        switch1.setThumbTintList(ColorStateList.valueOf(getResources().getColor(android.R.color.holo_orange_light)));
        //tint属性都可以做成 颜色选择器
        int[][] state = new int[2][];
        state[0] = new int[]{android.R.attr.state_checked};
        state[1] = new int[]{};
        int[] colors = new int[]{getResources().getColor(android.R.color.holo_orange_light), Color.parseColor("#000000")};
        ColorStateList stateList = new ColorStateList(state, colors);
        switch1.setThumbTintList(stateList);
        switch1.setTrackTintList(stateList);
    }
}